install.packages('corpcor')
install.packages('mctest')
require(mgcv)
require(corpcor)
require(mctest)


#Importing datasets

all_sellerdata  = read.csv(file="C:\\Users\\f00456n\\Documents\\Amazon Price Dynamics\\Data Scraped\\New Data\\Modeling data sets\\Microwave\\finalnumsellers_modeldata_microwave.csv", header=TRUE, sep=",")

modeldata = all_sellerdata[4:133,]

modeldata[is.na(modeldata)] = 0

attach(modeldata)

trainY = cbind(Y_amzn_lg,Y_amzn_psonic,Y_clust1_knmore,Y_clust2_psonic,Y_clust3_knmore,Y_clust4_psonic)


#AMAZON LG
data_amzn_lg = data.frame(cbind(Y_amzn_lg_lag1,Y_amzn_lg_lag2,Y_amzn_lg_lag3,Answered_Questionsknmore_lag1,
                                meanseller_star_clust2_lag1,
                                Answered_Questionslg_lag1,
                                meansalesrank_subcat_psonic_lag1,
                                meanuniq_ASIN_clust4_lag1,
                                Y_clust4_psonic_lag2,
                                #FBA_used3p_lag1,
                                meanseller_star_clust3_lag1,
                                Y_clust1_knmore_lag1,
                                Freeship_amzn_lag1,
                                Freeship_usedamzn_lag1,
                                salerank_subcat_knmore_lag1,
                                NUM_SKUSclust3_lag1,
                                knmore_topbrandlag1	,
                                lg_topbrandlag1	,
                                psonic_topbrandlag1	,
                                knmore_bottombrandlag1	,
                                lg_bottombrandlag1	,
                                psonic_bottombrandlag1	,
                                decr10per_clust1_knmore	,
                                decr10per_clust3_knmore	,
                                decr10per_amzn_psonic	,
                                decr10per_clust4_psonic	,
                                decr20per_clust1_knmore	,
                                decr20per_clust3_knmore	,
                                decr20per_amzn_psonic	,
                                decr20per_clust4_psonic	,
                                decr5per_clust1_knmore	,
                                decr5per_clust3_knmore,
                                decr5per_amzn_psonic	,
                                decr5per_clust4_psonic	,
                                incr10per_clust1_knmore	,
                                incr10per_clust3_knmore	,
                                incr10per_amzn_psonic	,
                                incr10per_clust4_psonic	,
                                incr20per_clust1_knmore	,
                                incr20per_clust3_knmore	,
                                incr20per_amzn_psonic	,
                                incr20per_clust4_psonic	,
                                incr5per_amzn_psonic	,
                                incr5per_clust4_psonic	,
                                decr10per_amzn_lg	,
                                decr10per_clust2_psonic	,
                                decr20per_amzn_lg	,
                                decr20per_clust2_psonic	,
                                decr5per_amzn_lg	,
                                decr5per_clust2_psonic	,
                                incr10per_amzn_lg	,
                                incr10per_clust2_psonic	,
                                incr20per_amzn_lg	,
                                incr20per_clust2_psonic	,
                                incr5per_amzn_lg	,
                                incr5per_clust2_psonic	,
                                weekend	,
                                #additional variables on product reviews
                                Product_reviews_knmore_lag1,
                                Product_reviews_lg_lag1,
                                maxProduct_reviews_psonic_lag1,
                                meanprodstar_psonic_lag1,
                                product_star_knmore_lag1,
                                product_star_lg_lag1,
                                buyboxprice_lg_lag1,
                                #additional variables based on JM RnR round 2
                                mean_starrateLG_3p_lag,
                                mean_primeLG_3p_lag,
                                seasonal_sale,
                                ###broken down external site price changes with lags
                                maxpricechange_HD,
                                maxpricechange_HDlag1,
                                maxpricechange_HDlag2,
                                maxpricechange_HDlag3,
                                maxpricechange_sears,
                                maxpricechange_searslag1,
                                maxpricechange_searslag2,
                                maxpricechange_searslag3,
                                maxpricechange_wm,
                                maxpricechange_wmlag1,
                                maxpricechange_wmlag2,
                                maxpricechange_wmlag3))


#ADDING FE & IE FOR SUBSTANTIVE INSIGHTS
gaml5_amznlg  = gam(Y_amzn_lg ~ 
                      Y_amzn_lg_lag2 + 
                      Answered_Questionslg_lag1+
                      #incr10per_clust3_knmore +
                      #decr5per_clust3_knmore+
                     # meansalesrank_subcat_psonic_lag1 +
                      #knmore_topbrandlag1	+
                      #knmore_topbrandlag1	+
                      #lg_topbrandlag1	+
                      #psonic_topbrandlag1	+
                      #knmore_bottombrandlag1	+
                      #knmore_bottombrandlag1	+
                      #lg_bottombrandlag1	+
                      #psonic_bottombrandlag1	+
                     ## decr10per_clust1_knmore	+
                      #decr10per_amzn_psonic	+
                      #decr10per_clust4_psonic	+
                      #decr20per_clust1_knmore	+
                     ## decr20per_amzn_psonic	+
                      #decr20per_clust4_psonic	+
                      #decr5per_clust1_knmore	+
                      #decr5per_amzn_psonic	+
                      #decr5per_clust4_psonic	+
                      #incr10per_clust1_knmore	+
                      #incr10per_amzn_psonic	+
                      #incr10per_clust4_psonic	+
                      #incr20per_clust1_knmore	+
                      #incr20per_amzn_psonic	+
                      #incr20per_clust4_psonic	+
                      #incr5per_clust1_knmore	+
                      ##incr5per_amzn_psonic	+
                      #incr5per_clust4_psonic	+
                      #decr10per_clust1_knmore	+
                      #decr10per_amzn_lg	+
                      #decr10per_clust2_psonic	+
                      #decr20per_clust1_knmore	+
                      #decr20per_amzn_lg	+
                      #decr20per_clust2_psonic	+
                      #decr5per_clust1_knmore	+
                      #decr5per_amzn_lg	+
                      #decr5per_clust2_psonic	+
                      #incr10per_clust1_knmore	+
                      incr10per_amzn_lg	+
                      buyboxprice_lg_lag1+
                      #incr10per_clust2_psonic	+
                      #incr20per_clust1_knmore	+
                      #incr20per_amzn_lg	+
                      #incr20per_clust2_psonic	+
                      #incr5per_clust1_knmore	+
                      #incr5per_amzn_lg	+
                      #incr5per_clust2_psonic	+
                      weekend	+
                      #additional variables on product reviews
                      Product_reviews_knmore_lag1 +
                      Product_reviews_lg_lag1+
                      maxProduct_reviews_psonic_lag1+
                      meanprodstar_psonic_lag1+
                      product_star_knmore_lag1+
                      product_star_lg_lag1+
                      #mean_starrateLG_3p_lag +
                     # mean_primeLG_3p_lag +
                      seasonal_sale +
                      ###4th round additions
                      #maxpricechange_HD +
                     # maxpricechange_HDlag1+
                    #  maxpricechange_HDlag2 +
                      maxpricechange_HDlag3 +
                      #maxpricechange_sears +
                      #maxpricechange_searslag1+
                    #  maxpricechange_searslag2+
                      maxpricechange_searslag3+
                      #maxpricechange_wm  +
                      maxpricechange_wmlag1 
                      #maxpricechange_wmlag2 
                      #maxpricechange_wmlag3
                    ,family= gaussian(link ='identity') ,data=data_amzn_lg,method="REML",optimizer=c("outer","newton"), fit = TRUE)


summary(gaml5_amznlg )



#AMAZON PANASONIC
data_amzn_psonic = data.frame(cbind(Y_amzn_psonic_lag1,Y_amzn_psonic_lag2,Y_amzn_psonic_lag3,
                                    Answered_Questionsknmore_lag1,
                                    meanseller_star_clust2_lag1,
                                    Answered_Questionslg_lag1,
                                    meansalesrank_subcat_psonic_lag1,
                                    meanuniq_ASIN_clust4_lag1,
                                    Y_clust4_psonic_lag2,
                                    #FBA_used3p_lag1,
                                    meanseller_star_clust3_lag1,
                                    Y_amzn_lg_lag2,
                                    Y_clust1_knmore_lag1,
                                    Freeship_amzn_lag1,
                                    Freeship_usedamzn_lag1,
                                    salerank_subcat_knmore_lag1,
                                    NUM_SKUSclust3_lag1,
                                    knmore_topbrandlag1	,
                                    lg_topbrandlag1	,
                                    psonic_topbrandlag1	,
                                    knmore_bottombrandlag1	,
                                    lg_bottombrandlag1	,
                                    psonic_bottombrandlag1	,
                                    decr10per_clust1_knmore	,
                                    decr10per_clust3_knmore	,
                                    decr10per_amzn_psonic	,
                                    decr10per_clust4_psonic	,
                                    decr20per_clust1_knmore	,
                                    decr20per_clust3_knmore	,
                                    decr20per_amzn_psonic	,
                                    decr20per_clust4_psonic	,
                                    decr5per_clust1_knmore	,
                                    decr5per_clust3_knmore,
                                    decr5per_amzn_psonic	,
                                    decr5per_clust4_psonic	,
                                    incr10per_clust1_knmore	,
                                    incr10per_clust3_knmore	,
                                    incr10per_amzn_psonic	,
                                    incr10per_clust4_psonic	,
                                    incr20per_clust1_knmore	,
                                    incr20per_clust3_knmore	,
                                    incr20per_amzn_psonic	,
                                    incr20per_clust4_psonic	,
                                    incr5per_amzn_psonic	,
                                    incr5per_clust4_psonic	,
                                    decr10per_amzn_lg	,
                                    decr10per_clust2_psonic	,
                                    decr20per_amzn_lg	,
                                    decr20per_clust2_psonic	,
                                    decr5per_amzn_lg	,
                                    decr5per_clust2_psonic	,
                                    incr10per_amzn_lg	,
                                    incr10per_clust2_psonic	,
                                    incr20per_amzn_lg	,
                                    incr20per_clust2_psonic	,
                                    incr5per_amzn_lg	,
                                    incr5per_clust2_psonic	,
                                    weekend	,
                                    #additional variables on product reviews
                                    meanbuyboxprice_psonic_lag1,
                                    Product_reviews_knmore_lag1,
                                    Product_reviews_lg_lag1,
                                    maxProduct_reviews_psonic_lag1,
                                    meanprodstar_psonic_lag1,
                                    product_star_knmore_lag1,
                                    product_star_lg_lag1,
                                    #additional variables based on JM RnR round 2
                                    mean_starratePSN1_3p_lag,
                                    mean_starratePSN2_3p_lag,
                                    mean_starratePSN3_3p_lag,
                                    mean_primePSN1_3p_lag,
                                    mean_primePSN2_3p_lag,
                                    mean_primePSN3_3p_lag,
                                    seasonal_sale,
                                    ###broken down external site price changes with lags
                                    maxpricechange_HD,
                                    maxpricechange_HDlag1,
                                    maxpricechange_HDlag2,
                                    maxpricechange_HDlag3,
                                    maxpricechange_sears,
                                    maxpricechange_searslag1,
                                    maxpricechange_searslag2,
                                    maxpricechange_searslag3,
                                    maxpricechange_wm,
                                    maxpricechange_wmlag1,
                                    maxpricechange_wmlag2,
                                    maxpricechange_wmlag3))
                                    

#ADDING FE & IE FOR SUBSTANTIVE INSIGHTS
gamtop_amznpsonic= gam(Y_amzn_psonic ~ 
                         Y_amzn_psonic_lag1+
                       #  Answered_Questionsknmore_lag1+
                         meanseller_star_clust2_lag1+
                         s(meansalesrank_subcat_psonic_lag1)
                       + NUM_SKUSclust3_lag1
                     #  +Y_clust4_psonic_lag2
                      # + Y_clust1_knmore_lag1
                       + Freeship_usedamzn_lag1+
                       meanbuyboxprice_psonic_lag1+
                        # knmore_topbrandlag1	+
                        # psonic_topbrandlag1	+
                         #knmore_bottombrandlag1	+
                       #decr10per_clust1_knmore	+
                       #decr10per_amzn_psonic	+
                       #decr10per_clust4_psonic	+
                       #decr20per_clust1_knmore	+
                       #decr20per_amzn_psonic	+
                       #decr20per_clust4_psonic	+
                       #decr5per_clust1_knmore	+
                       decr5per_amzn_psonic	+
                       #decr5per_clust4_psonic	+
                       #incr10per_clust1_knmore	+
                       incr10per_amzn_psonic	+
                       #incr10per_clust4_psonic	+
                       #incr20per_clust1_knmore	+
                       #incr20per_amzn_psonic	+
                       #incr20per_clust4_psonic	+
                       #incr5per_clust1_knmore	+
                       incr5per_amzn_psonic	+
                       incr5per_clust4_psonic	+
                       #decr5per_clust3_knmore +
                       #decr10per_clust1_knmore	+
                       #decr10per_amzn_lg	+
                       #decr10per_clust2_psonic	+
                       #decr20per_clust1_knmore	+
                       #decr20per_amzn_lg	+
                       #decr20per_clust2_psonic	+
                      # decr5per_clust1_knmore	+
                       #decr5per_amzn_lg	+
                       #decr5per_clust2_psonic	+
                       #incr10per_clust1_knmore	+
                       #incr10per_amzn_lg	+
                       #incr10per_clust2_psonic	+
                       #incr20per_clust1_knmore	+
                       #incr20per_amzn_lg	+
                       #incr20per_clust2_psonic	+
                       #incr5per_clust1_knmore	+
                       #incr5per_amzn_lg	+
                       #incr5per_clust2_psonic	+
                       #additional variables on product reviews
                       Product_reviews_knmore_lag1 +
                       Product_reviews_lg_lag1+
                       maxProduct_reviews_psonic_lag1+
                       meanprodstar_psonic_lag1+
                       product_star_knmore_lag1+
                       product_star_lg_lag1+
                       weekend	+ 
                       mean_starratePSN1_3p_lag+
                       mean_starratePSN2_3p_lag+
                       mean_primePSN2_3p_lag+
                     seasonal_sale +
                       ###4th round additions
                       maxpricechange_HD +
                      # maxpricechange_HDlag1+
                      # maxpricechange_HDlag2 +
                      # maxpricechange_HDlag3 +
                       maxpricechange_sears +
                      # maxpricechange_searslag1+
                      # maxpricechange_searslag2+
                       #maxpricechange_searslag3+
                       maxpricechange_wm  
                       #maxpricechange_wmlag1 
                       #maxpricechange_wmlag2 +
                       #maxpricechange_wmlag3
                       ,family= gaussian(link ='identity') ,data=data_amzn_psonic,method="REML",optimizer=c("outer","newton"), fit = TRUE)


summary(gamtop_amznpsonic)


#CLUSTER 1

data_clust1 = data.frame(cbind(Y_clust1_knmore_lag1,
                               Y_clust1_knmore_lag2,
                               Y_clust1_knmore_lag3,
                               Answered_Questionsknmore_lag1,
                               meanseller_star_clust2_lag1,
                               Answered_Questionslg_lag1,
                               meansalesrank_subcat_psonic_lag1,
                               meanuniq_ASIN_clust4_lag1,
                               Y_clust4_psonic_lag2,
                               #FBA_used3p_lag1,
                               meanseller_star_clust3_lag1,
                               Y_amzn_lg_lag2,
                               #Y_clust1_knmore_lag1,
                               Freeship_amzn_lag1,
                               Freeship_usedamzn_lag1,
                               salerank_subcat_knmore_lag1,
                               NUM_SKUSclust3_lag1,
                               knmore_topbrandlag1	,
                               knmore_topbrandlag1	,
                               lg_topbrandlag1	,
                               psonic_topbrandlag1	,
                               knmore_bottombrandlag1	,
                               knmore_bottombrandlag1	,
                               lg_bottombrandlag1	,
                               psonic_bottombrandlag1	,
                               decr10per_clust1_knmore	,
                               decr10per_clust3_knmore	,
                               decr10per_amzn_psonic	,
                               decr10per_clust4_psonic	,
                               decr20per_clust1_knmore	,
                               decr20per_clust3_knmore	,
                               decr20per_amzn_psonic	,
                               decr20per_clust4_psonic	,
                               decr5per_clust1_knmore	,
                               decr5per_clust3_knmore,
                               decr5per_amzn_psonic	,
                               decr5per_clust4_psonic	,
                               incr10per_clust1_knmore	,
                               incr10per_clust3_knmore	,
                               incr10per_amzn_psonic	,
                               incr10per_clust4_psonic	,
                               incr20per_clust1_knmore	,
                               incr20per_clust3_knmore	,
                               incr20per_amzn_psonic	,
                               incr20per_clust4_psonic	,
                               incr5per_amzn_psonic	,
                               incr5per_clust4_psonic	,
                               decr10per_amzn_lg	,
                               decr10per_clust2_psonic	,
                               decr20per_amzn_lg	,
                               decr20per_clust2_psonic	,
                               decr5per_amzn_lg	,
                               decr5per_clust2_psonic	,
                               incr10per_amzn_lg	,
                               incr10per_clust2_psonic	,
                               incr20per_amzn_lg	,
                               incr20per_clust2_psonic	,
                               incr5per_amzn_lg	,
                               incr5per_clust2_psonic	,
                               weekend	,
                               #additional variables on product reviews
                               buyboxprice_knmore_lag1,
                               Product_reviews_knmore_lag1,
                               Product_reviews_lg_lag1,
                               maxProduct_reviews_psonic_lag1,
                               meanprodstar_psonic_lag1,
                               product_star_knmore_lag1,
                               product_star_lg_lag1,
                               #additional variables based on JM RnR round 2
                               mean_starrateKNM_3p_lag,
                               mean_primeKNM_3p_lag,
                               salerank_knmore_lag1,
                               seasonal_sale,
                               ###broken down external site price changes with lags
                               maxpricechange_HD,
                               maxpricechange_HDlag1,
                               maxpricechange_HDlag2,
                               maxpricechange_HDlag3,
                               maxpricechange_sears,
                               maxpricechange_searslag1,
                               maxpricechange_searslag2,
                               maxpricechange_searslag3,
                               maxpricechange_wm,
                               maxpricechange_wmlag1,
                               maxpricechange_wmlag2,
                               maxpricechange_wmlag3))



#ADDING FE & IE FOR SUBSTANTIVE INSIGHTS
gam6_clust1 = gam(Y_clust1_knmore ~  Y_clust1_knmore_lag1 +
                    incr5per_clust1_knmore +
                      incr10per_clust1_knmore +
                    buyboxprice_knmore_lag1+
                  decr10per_clust3_knmore+
                  incr10per_clust3_knmore+
                  incr10per_amzn_psonic+
                  incr5per_clust3_knmore+
                  incr5per_amzn_psonic+
                  weekend+
                  Product_reviews_knmore_lag1+
                  salerank_knmore_lag1+
                  meanprodstar_psonic_lag1+
                  product_star_knmore_lag1+
                  seasonal_sale+
                  maxpricechange_HDlag2+
                  maxpricechange_searslag1+
                  maxpricechange_wmlag1
                  
                  ,family= gaussian(link = 'identity') ,data=data_clust1,method="REML",optimizer=c("outer","newton"), fit = TRUE)

summary(gam6_clust1)

#CLUSTER 2

data_clust2 = data.frame(cbind(Y_clust2_psonic_lag1,Y_clust2_psonic_lag2,Y_clust2_psonic_lag3,
                               Answered_Questionsknmore_lag1,
                               meanseller_star_clust2_lag1,
                               Answered_Questionslg_lag1,
                               meansalesrank_subcat_psonic_lag1,
                               meanuniq_ASIN_clust4_lag1,
                               Y_clust4_psonic_lag2,
                               meanseller_star_clust3_lag1,
                               Y_amzn_lg_lag2,
                               Y_clust1_knmore_lag1,
                               Freeship_amzn_lag1,
                               Freeship_usedamzn_lag1,
                               salerank_subcat_knmore_lag1,
                               NUM_SKUSclust3_lag1,
                               knmore_topbrandlag1	,
                               lg_topbrandlag1	,
                               psonic_topbrandlag1	,
                               knmore_bottombrandlag1	,
                               lg_bottombrandlag1	,
                               psonic_bottombrandlag1	,
                               decr10per_clust1_knmore	,
                               decr10per_amzn_psonic	,
                               decr10per_clust4_psonic	,
                               decr20per_clust1_knmore	,
                               decr20per_amzn_psonic	,
                               decr20per_clust4_psonic	,
                               decr5per_clust1_knmore	,
                               decr5per_amzn_psonic	,
                               decr5per_clust4_psonic	,
                               incr10per_clust1_knmore	,
                               incr10per_amzn_psonic	,
                               incr10per_clust4_psonic	,
                               incr20per_clust1_knmore	,
                               incr20per_amzn_psonic	,
                               incr20per_clust4_psonic	,
                               incr5per_clust1_knmore	,
                               incr5per_amzn_psonic	,
                               incr5per_clust4_psonic	,
                               decr10per_amzn_lg	,
                               decr10per_clust2_psonic	,
                               decr20per_amzn_lg	,
                               decr20per_clust2_psonic	,
                               decr5per_amzn_lg	,
                               decr5per_clust2_psonic	,
                               incr10per_amzn_lg	,
                               incr10per_clust2_psonic	,
                               incr20per_amzn_lg	,
                               incr20per_clust2_psonic	,
                               incr5per_amzn_lg	,
                               incr5per_clust2_psonic	,
                               weekend	,
                               #additional variables on product reviews
                               meanbuyboxprice_psonic_lag1,
                               Product_reviews_knmore_lag1,
                               Product_reviews_lg_lag1,
                               maxProduct_reviews_psonic_lag1,
                               meanprodstar_psonic_lag1,
                               product_star_knmore_lag1,
                               product_star_lg_lag1,
                               #additional variables based on JM RnR round 2
                               mean_starratePSN1_3p_lag,
                               mean_starratePSN2_3p_lag,
                               mean_starratePSN3_3p_lag,
                               mean_primePSN1_3p_lag,
                               mean_primePSN2_3p_lag,
                               mean_primePSN3_3p_lag,
                               seasonal_sale,
                               ###broken down external site price changes with lags
                               maxpricechange_HD,
                               maxpricechange_HDlag1,
                               maxpricechange_HDlag2,
                               maxpricechange_HDlag3,
                               maxpricechange_sears,
                               maxpricechange_searslag1,
                               maxpricechange_searslag2,
                               maxpricechange_searslag3,
                               maxpricechange_wm,
                               maxpricechange_wmlag1,
                               maxpricechange_wmlag2,
                               maxpricechange_wmlag3))


#ADDING FE & IE FOR SUBSTANTIVE INSIGHTS
gam17_clust2 = gam(Y_clust2_psonic ~  Y_clust2_psonic_lag1 + 
                     meanseller_star_clust2_lag1+
                     #Y_clust4_psonic_lag2+
                    # Y_clust1_knmore_lag1+
                    # decr10per_clust3_knmore+
                    # incr10per_clust3_knmore+
                    # psonic_topbrandlag1	+
                     #knmore_bottombrandlag1	+
                     #decr10per_clust1_knmore	+
                    # decr10per_amzn_psonic	+
                     #decr10per_clust4_psonic	+
                     #decr20per_clust1_knmore	+
                     #decr20per_amzn_psonic	+
                     #decr20per_clust4_psonic	+
                     #decr5per_clust1_knmore	+
                     #decr5per_amzn_psonic	+
                     #decr5per_clust4_psonic	+
                     #incr10per_clust1_knmore	+
                     ##incr10per_amzn_psonic	+
                     #incr10per_clust4_psonic	+
                     #incr20per_clust1_knmore	+
                     #incr20per_amzn_psonic	+
                     #incr20per_clust4_psonic	+
                     ##incr5per_clust1_knmore	+
                    incr5per_amzn_psonic	+
                   #  incr5per_clust4_psonic	+
                     #decr10per_clust1_knmore	+
                    ## decr10per_amzn_lg	+
                     #decr10per_clust2_psonic	+
                     #decr20per_clust1_knmore	+
                     #decr20per_amzn_lg	+
                     #decr20per_clust2_psonic	+
                     ##decr5per_clust1_knmore	+
                     #decr5per_amzn_lg	+
                     #decr5per_clust2_psonic	+
                     #incr10per_clust1_knmore	+
                   #  incr10per_amzn_lg	+
                     #incr10per_clust2_psonic	+
                     #incr20per_clust1_knmore	+
                     #incr20per_amzn_lg	+
                     #incr20per_clust2_psonic	+
                     #incr5per_clust1_knmore	+
                     #incr5per_amzn_lg	+
                     #incr5per_clust2_psonic	+
                     weekend +
                     #additional variables on product reviews
                     meanbuyboxprice_psonic_lag1 + 
                   #  Product_reviews_knmore_lag1 +
                  #   Product_reviews_lg_lag1 +
                     maxProduct_reviews_psonic_lag1 +
                     meanprodstar_psonic_lag1+
                    # product_star_knmore_lag1+
                    # product_star_lg_lag1+
                    # mean_starratePSN1_3p_lag + 
                     mean_starratePSN2_3p_lag +
                    # mean_starratePSN3_3p_lag +
                    # mean_primePSN1_3p_lag +
                    # mean_primePSN2_3p_lag +
                    # mean_primePSN3_3p_lag +
                    seasonal_sale+
                     ###4th round additions
                    # maxpricechange_HD +
                    maxpricechange_HDlag1+
                    # maxpricechange_HDlag2 +
                  #   maxpricechange_HDlag3 +
                    # maxpricechange_sears +
                  #  maxpricechange_searslag1+
                   #  maxpricechange_searslag2+
                    maxpricechange_searslag3+
                   #  maxpricechange_wm  +
                     maxpricechange_wmlag1 
                    # maxpricechange_wmlag2 +
                    # maxpricechange_wmlag3
                   ,family= gaussian(link = 'identity') ,data=data_clust2,method="REML",optimizer=c("outer","newton"), fit = TRUE)

summary(gam17_clust2)

#CLUSTER 3

data_clust3 = data.frame(cbind(Y_clust3_knmore_lag1, 
                               Y_clust3_knmore_lag2, 
                               Y_clust3_knmore_lag3,
                               Answered_Questionsknmore_lag1,
                               meanseller_star_clust2_lag1,
                               Answered_Questionslg_lag1,
                               meansalesrank_subcat_psonic_lag1,
                               meanuniq_ASIN_clust4_lag1,
                               Y_clust4_psonic_lag2,
                               #FBA_used3p_lag1,
                               meanseller_star_clust3_lag1,
                               Y_amzn_lg_lag2,
                               #Y_clust1_knmore_lag1,
                               Freeship_amzn_lag1,
                               Freeship_usedamzn_lag1,
                               salerank_subcat_knmore_lag1,
                               NUM_SKUSclust3_lag1,
                               knmore_topbrandlag1	,
                               knmore_topbrandlag1	,
                               lg_topbrandlag1	,
                               psonic_topbrandlag1	,
                               knmore_bottombrandlag1	,
                               knmore_bottombrandlag1	,
                               lg_bottombrandlag1	,
                               psonic_bottombrandlag1	,
                               decr10per_clust1_knmore	,
                               decr10per_clust3_knmore	,
                               decr10per_amzn_psonic	,
                               decr10per_clust4_psonic	,
                               decr20per_clust1_knmore	,
                               decr20per_clust3_knmore	,
                               decr20per_amzn_psonic	,
                               decr20per_clust4_psonic	,
                               decr5per_clust1_knmore	,
                               decr5per_clust3_knmore,
                               decr5per_amzn_psonic	,
                               decr5per_clust4_psonic	,
                               incr10per_clust1_knmore	,
                               incr10per_clust3_knmore	,
                               incr10per_amzn_psonic	,
                               incr10per_clust4_psonic	,
                               incr20per_clust1_knmore	,
                               incr20per_clust3_knmore	,
                               incr20per_amzn_psonic	,
                               incr20per_clust4_psonic	,
                               incr5per_amzn_psonic	,
                               incr5per_clust4_psonic	,
                               decr10per_amzn_lg	,
                               decr10per_clust2_psonic	,
                               decr20per_amzn_lg	,
                               decr20per_clust2_psonic	,
                               decr5per_amzn_lg	,
                               decr5per_clust2_psonic	,
                               incr10per_amzn_lg	,
                               incr10per_clust2_psonic	,
                               incr20per_amzn_lg	,
                               incr20per_clust2_psonic	,
                               incr5per_amzn_lg	,
                               incr5per_clust2_psonic	,
                               weekend	,
                               #additional variables on product reviews
                               buyboxprice_knmore_lag1,
                               Product_reviews_knmore_lag1,
                               Product_reviews_lg_lag1,
                               maxProduct_reviews_psonic_lag1,
                               meanprodstar_psonic_lag1,
                               product_star_knmore_lag1,
                               product_star_lg_lag1,
                               #additional variables based on JM RnR round 2
                               mean_starrateKNM_3p_lag,
                               mean_primeKNM_3p_lag,
                               salerank_knmore_lag1,
                               seasonal_sale,
                               ###broken down external site price changes with lags
                               maxpricechange_HD,
                               maxpricechange_HDlag1,
                               maxpricechange_HDlag2,
                               maxpricechange_HDlag3,
                               maxpricechange_sears,
                               maxpricechange_searslag1,
                               maxpricechange_searslag2,
                               maxpricechange_searslag3,
                               maxpricechange_wm,
                               maxpricechange_wmlag1,
                               maxpricechange_wmlag2,
                               maxpricechange_wmlag3))


#ADDING FE & IE FOR SUBSTANTIVE INSIGHTS
gam16_clust3 = gam(Y_clust3_knmore ~  
                     Y_clust3_knmore_lag2+
                     #Answered_Questionslg_lag1+
                    # meansalesrank_subcat_psonic_lag1 +
                   #  Y_clust4_psonic_lag2+
                     meanseller_star_clust3_lag1+
                     Y_amzn_lg_lag2 +
                     buyboxprice_knmore_lag1+
                    # decr10per_clust3_knmore+
                     incr10per_clust3_knmore+
                  #   decr5per_clust3_knmore +
                     # psonic_topbrandlag1	+
                     #knmore_bottombrandlag1	+
                    # decr10per_clust1_knmore	+
                     #decr10per_amzn_psonic	+
                     #decr10per_clust4_psonic	+
                     #decr20per_clust1_knmore	+
                     #decr20per_amzn_psonic	+
                     #decr20per_clust4_psonic	+
                     #decr5per_clust1_knmore	+
                     #decr5per_amzn_psonic	+
                     #decr5per_clust4_psonic	+
                     incr10per_clust1_knmore	+
                    # incr10per_amzn_psonic	+
                     #incr10per_clust4_psonic	+
                     #incr20per_clust1_knmore	+
                     #incr20per_amzn_psonic	+
                     #incr20per_clust4_psonic	+
                    # incr5per_clust1_knmore	+
                     #incr5per_amzn_psonic	+
                     #incr5per_clust4_psonic	+
                     #decr10per_clust1_knmore	+
                   #  decr10per_amzn_lg	+
                     #decr10per_clust2_psonic	+
                     #decr20per_clust1_knmore	+
                    # decr20per_amzn_lg	+
                     #decr20per_clust2_psonic	+
                     #decr5per_clust1_knmore	+
                    # decr5per_amzn_lg	+
                     #decr5per_clust2_psonic	+
                     #incr10per_clust1_knmore	+
                   #  incr10per_amzn_lg	+
                     #incr10per_clust2_psonic	+
                     #incr20per_clust1_knmore	+
                   #  incr20per_amzn_lg	+
                     #incr20per_clust2_psonic	+
                     #incr5per_clust1_knmore	+
                   #  incr5per_amzn_lg	+
                     #incr5per_clust2_psonic	+
                     weekend +
                     #additional variables on product reviews
                    # Product_reviews_knmore_lag1 +
                  #   salerank_knmore_lag1 +
                     # Product_reviews_lg_lag1 +
                     #  maxProduct_reviews_psonic_lag1 +
                   #  meanprodstar_psonic_lag1+
                   #  product_star_knmore_lag1+
                     #  product_star_lg_lag1+
                     #  mean_starrateKNM_3p_lag+
                     seasonal_sale +
                     ###4th round additions
                     # maxpricechange_HD +
                    #  maxpricechange_HDlag1+
                    # maxpricechange_HDlag2 +
                     #maxpricechange_HDlag3 +
                     # maxpricechange_sears +
                     maxpricechange_searslag2+
                     # maxpricechange_searslag2+
                     #maxpricechange_searslag3+
                     #maxpricechange_wm  +
                     maxpricechange_wmlag2 
                   #maxpricechange_wmlag2 +
                   #maxpricechange_wmlag3
                   ,family= gaussian(link = 'identity') ,data=data_clust3,method="REML",optimizer=c("outer","newton"), fit = TRUE)


summary(gam16_clust3)

#CLUSTER 4

data_clust4 = data.frame(cbind(Y_clust4_psonic_lag1,
                               Y_clust4_psonic_lag2,
                               Y_clust4_psonic_lag3,
                               Answered_Questionsknmore_lag1,
                               meanseller_star_clust2_lag1,
                               Answered_Questionslg_lag1,
                               meansalesrank_subcat_psonic_lag1,
                               meanuniq_ASIN_clust4_lag1,
                               Y_clust4_psonic_lag2,
                               meanseller_star_clust3_lag1,
                               Y_amzn_lg_lag2,
                               Y_clust1_knmore_lag1,
                               Freeship_amzn_lag1,
                               Freeship_usedamzn_lag1,
                               salerank_subcat_knmore_lag1,
                               NUM_SKUSclust3_lag1,
                               knmore_topbrandlag1	,
                               lg_topbrandlag1	,
                               psonic_topbrandlag1	,
                               knmore_bottombrandlag1	,
                               lg_bottombrandlag1	,
                               psonic_bottombrandlag1	,
                               decr10per_clust1_knmore	,
                               decr10per_clust3_knmore	,
                               decr10per_amzn_psonic	,
                               decr10per_clust4_psonic	,
                               decr20per_clust1_knmore	,
                               decr20per_clust3_knmore	,
                               decr20per_amzn_psonic	,
                               decr20per_clust4_psonic	,
                               decr5per_clust1_knmore	,
                               decr5per_clust3_knmore,
                               decr5per_amzn_psonic	,
                               decr5per_clust4_psonic	,
                               incr10per_clust1_knmore	,
                               incr10per_clust3_knmore	,
                               incr10per_amzn_psonic	,
                               incr10per_clust4_psonic	,
                               incr20per_clust1_knmore	,
                               incr20per_clust3_knmore	,
                               incr20per_amzn_psonic	,
                               incr20per_clust4_psonic	,
                               incr5per_amzn_psonic	,
                               incr5per_clust4_psonic	,
                               decr10per_amzn_lg	,
                               decr10per_clust2_psonic	,
                               decr20per_amzn_lg	,
                               decr20per_clust2_psonic	,
                               decr5per_amzn_lg	,
                               decr5per_clust2_psonic	,
                               incr10per_amzn_lg	,
                               incr10per_clust2_psonic	,
                               incr20per_amzn_lg	,
                               incr20per_clust2_psonic	,
                               incr5per_amzn_lg	,
                               incr5per_clust2_psonic	,
                               weekend	,
                               #additional variables on product reviews
                               meanbuyboxprice_psonic_lag1,
                               Product_reviews_knmore_lag1,
                               Product_reviews_lg_lag1,
                               maxProduct_reviews_psonic_lag1,
                               meanprodstar_psonic_lag1,
                               product_star_knmore_lag1,
                               product_star_lg_lag1,
                               #additional variables based on JM RnR round 2
                               mean_starratePSN1_3p_lag,
                               mean_starratePSN2_3p_lag,
                               mean_starratePSN3_3p_lag,
                               mean_primePSN1_3p_lag,
                               mean_primePSN2_3p_lag,
                               mean_primePSN3_3p_lag,
                               seasonal_sale,
                               ###broken down external site price changes with lags
                               maxpricechange_HD,
                               maxpricechange_HDlag1,
                               maxpricechange_HDlag2,
                               maxpricechange_HDlag3,
                               maxpricechange_sears,
                               maxpricechange_searslag1,
                               maxpricechange_searslag2,
                               maxpricechange_searslag3,
                               maxpricechange_wm,
                               maxpricechange_wmlag1,
                               maxpricechange_wmlag2,
                               maxpricechange_wmlag3))


#ADDING FE & IE FOR SUBSTANTIVE INSIGHTS

gam15_clust4  = gam(Y_clust4_psonic ~ 
                      Y_clust4_psonic_lag1+
                      Y_clust4_psonic_lag2+
                     # Answered_Questionsknmore_lag1+
                      meanseller_star_clust2_lag1 +
                      Answered_Questionslg_lag1+
                      meansalesrank_subcat_psonic_lag1+
                      meanbuyboxprice_psonic_lag1 +
                     # Y_clust1_knmore_lag1+
                      salerank_subcat_knmore_lag1+
                     # incr10per_clust3_knmore+
                      #psonic_topbrandlag1	+
                      #knmore_bottombrandlag1	+
                      #decr10per_clust1_knmore	+
                      #decr10per_amzn_psonic	+
                      #decr10per_clust4_psonic	+
                      #decr20per_clust1_knmore	+
                      #decr20per_amzn_psonic	+
                      #decr20per_clust4_psonic	+
                      #decr5per_clust1_knmore	+
                      #decr5per_amzn_psonic	+
                      #decr5per_clust4_psonic	+
                      #incr10per_clust1_knmore	+
                      #incr10per_amzn_psonic	+
                      #incr10per_clust4_psonic	+
                      #incr20per_clust1_knmore	+
                      #incr20per_amzn_psonic	+
                      #incr20per_clust4_psonic	+
                      #incr5per_clust1_knmore	+
                     # incr5per_amzn_psonic	+
                      #incr5per_clust4_psonic	+
                      #decr10per_clust1_knmore	+
                      decr5per_amzn_psonic	+
                      decr20per_amzn_psonic +
                    #  decr5per_amzn_psonic +
                      #decr10per_clust2_psonic	+
                      #decr20per_clust1_knmore	+
                      #decr20per_amzn_lg	+
                      #decr20per_clust2_psonic	+
                      #decr5per_clust1_knmore	+
                      #decr5per_amzn_lg	+
                      #decr5per_clust2_psonic	+
                      #incr10per_clust1_knmore	+
                      #incr10per_amzn_lg	+
                      #incr10per_clust2_psonic	+
                      #incr20per_clust1_knmore	+
                      #incr20per_amzn_lg	+
                      #incr20per_clust2_psonic	+
                      #incr5per_clust1_knmore	+
                      #incr5per_amzn_lg	+
                      #incr5per_clust2_psonic	+
                      weekend +
                      #additional variables on product reviews
                     # Product_reviews_knmore_lag1 +
                     # Product_reviews_lg_lag1 +
                      maxProduct_reviews_psonic_lag1 +
                      meanprodstar_psonic_lag1+
                     # product_star_knmore_lag1+
                      product_star_lg_lag1+
                      #additional variables based on JM RnR round 2
                     # mean_starratePSN2_3p_lag +
                      # mean_starratePSN3_3p_lag +
                      # mean_primePSN1_3p_lag +
                      # mean_primePSN2_3p_lag +
                      # mean_primePSN3_3p_lag +
                    seasonal_sale+
                      ###4th round additions
                     # maxpricechange_HD +
                    #  maxpricechange_HDlag1+
                      maxpricechange_HDlag2 +
                     # maxpricechange_HDlag3 +
                     # maxpricechange_sears +
                      maxpricechange_searslag1+
                     # maxpricechange_searslag2+
                     # maxpricechange_searslag3+
                     # maxpricechange_wm  +
                     # maxpricechange_wmlag1 +
                      maxpricechange_wmlag2 
                      #maxpricechange_wmlag3
                    ,family= gaussian(link = 'identity') ,data=data_clust4,method="REML",optimizer=c("outer","newton"), fit = TRUE)

summary(gam15_clust4)


